package com.aso.common.utils;

import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.aso.common.http.utils.HttpUtils;
import com.itsdf07.alog.ALog;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 定时器
 * Created by itsdf07 on 2017/7/19.
 */

public class TimerUtils {
    public interface ITimerCallback {
        void callback(String result);
    }

    private static TimerUtils instance;

    /**
     * 定时器默认延迟启用时长：毫秒
     */
    private long TIMER_DELAYTIME_DEFAULT = 1000;
    /**
     * 定时器默认间隔时长：毫秒
     */
    private long TIMER_INTERVALTIME_DEFAULT = 10000;
    private int mTimeSec;
    private int mTimeMin;
    private int mTimeHour;

    Timer mRunTimer;

    public static TimerUtils getInstance() {
        if (null == instance) {
            instance = new TimerUtils();
        }
        return instance;
    }

    private TimerUtils() {
        mRunTimer = new Timer();
    }

    /**
     * 启动时钟定时器
     */
    public void startTimer(ITimerCallback callback) {
        MyTimerTask task = new MyTimerTask(callback);
        /**
         * param1：TimerTask
         * param2：延迟时长后开始执行param1
         * param3：定时器执行间隔
         */
        mRunTimer.schedule(task, TIMER_DELAYTIME_DEFAULT, TIMER_INTERVALTIME_DEFAULT);// 每隔1s更新一下时间
    }

    /**
     * 停止时钟定时器
     */
    private void stopTimer() {
        if (mRunTimer != null) {
            mRunTimer.cancel();
            mRunTimer = null;
        }
        mTimeSec = 0;
        mTimeMin = 0;
        mTimeHour = 0;
    }

    private class MyTimerTask extends TimerTask {
        private ITimerCallback mTimerCallback;

        public MyTimerTask(ITimerCallback callback) {
            mTimerCallback = callback;
        }

        @Override
        public void run() {
            mTimeSec++;
            if (mTimeSec == 60) {
                mTimeSec = 0;
                mTimeMin++;
            }
            if (mTimeMin == 60) {
                mTimeMin = 0;
                mTimeHour++;
            }
            if (mTimeHour == 24) {
                mTimeSec = 0;
                mTimeMin = 0;
                mTimeHour = 0;
            }
            if (null != mTimerCallback) {
                mTimerCallback.callback(mTimeHour + " : " + mTimeMin + " : " + mTimeSec);
            }
        }
    }

    public static void testTimerThreadInBack() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                TimerUtils.getInstance().startTimer(new TimerUtils.ITimerCallback() {
                    @Override
                    public void callback(String result) {
                        String url = "http://192.168.0.130:8080/TestWebProject/test";
                        String date = DateTimeUtils.getDate("yyyy年MM月dd日 HH:mm:ss");
                        HttpUtils.postAsyn(url,
                                DateTimeUtils.getDate("yyyy年MM月dd日 HH:mm:ss") + "-" + DeviceUtils.getDeviceUtils().getModel() + "-IMEI = " + DeviceUtils.getDeviceUtils().getIMEI(),
                                new HttpCallbackImpl<BaseBean>() {
                                    @Override
                                    public void onStart() {

                                    }

                                    @Override
                                    public void onSuccess(BaseBean baseBean) {
                                        ALog.d("dfsu", "onSuccess：" + baseBean.getCode() + ":" + baseBean.getDesc());
                                    }

                                    @Override
                                    public void onFailureResult(BaseBean bean) {

                                        ALog.d("dfsu", "onFailure：" + bean.getCode() + ":" + bean.getDesc());
                                    }

                                    @Override
                                    public void onFinish() {

                                    }
                                });
                    }
                });

            }
        }, "testTimerThreadInBack").start();
    }

    public static void testThreadInBack() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; ; ) {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String date = DateTimeUtils.getDate("yyyy年MM月dd日 HH:mm:ss");
                    ALog.d("dfsu", "testThreadInBack date = %s", date);
                }
            }
        }, "testThreadInBack").start();
    }
}
